package gui.QualityControl;

import javax.lang.model.element.QualifiedNameable;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import java.awt.Color;
import java.awt.CardLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JButton;

import entities.FailReport;
import entities.FailSet;
import entities.ProductionOrder;
import entities.QCReport;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.SwingConstants;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;

import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.Font;

/**
 * @author Idan
 * GUI create report panel.
 * Each order type has it's own fitting report format, which is loaded dynamically
 * according to the chosen order.
 * 
 */
public class QCReportPanel extends JPanel 
{
	private JTextField orderIDTextField;
	private JTextField orderDateTextField;
	private JTextField typeTextField;
	private JTextField quantityTextField;
	private JTextField diopterTextField;
	private JTextField minus10DeviationTextField;
	private JTextField minus05DeviationTextField;
	private JTextField plus05DeviationTextField;
	private JTextField plus10DeviationTextField;
	private JTextField elseDeviationTextField;
	private JTextField steralizationTextField;
	private CardLayout reportLayoutManager;
	public String currentReport;
	
	private QCHydrophilicReport hydrophilicReport;
	private QCHydrophobicReport hydrophobicReport;
	private QCPMMAReport pmmaReport;
	private QCPresbiaReport presbiaReport;
	private JPanel masterPanel;
	private JPanel deviationsJPanel;
	private JPanel steralizationPanel;
	private QCFrame myFrame;
	private JButton createReportButton;
	private JScrollPane scrollPane;

	

	public QCReportPanel(QCFrame qualityControlFrame) 
	{
		setBackground(new Color(173, 216, 230));
		this.myFrame = qualityControlFrame;
		setLayout(new MigLayout("", "[][grow][]", "[grow][]"));
		
		
		
		JPanel panel = new JPanel(){@Override
			public void paintComponent(Graphics g) {
	        Graphics2D g2d = (Graphics2D) g;
	        Color color1 = getBackground();
	        Color color2 = color1.darker();
	        int w = getWidth();
	        int h = getHeight(); 
	        GradientPaint gp = new GradientPaint(
	                0, 0, color1,
	                0, h, color2);

	        g2d.setPaint(gp);
	        g2d.fillRect(0, 0, w, h);
	        }};
		panel.setBackground(new Color(173, 216, 230));

		panel.setBorder(new TitledBorder(null, "\u05D3\u05D5\u05D7 \u05D1\u05E7\u05E8\u05EA \u05D0\u05D9\u05DB\u05D5\u05EA", TitledBorder.RIGHT, TitledBorder.TOP, null, null));
		panel.setLayout(new MigLayout("", "[grow][][grow][grow][][grow]", "[][][][grow][::150px,grow]"));
		
		add(panel, "cell 1 0,grow");
		
		diopterTextField = new JTextField();
		diopterTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		diopterTextField.setHorizontalAlignment(SwingConstants.CENTER);
		diopterTextField.setEditable(false);
		diopterTextField.setBackground(Color.LIGHT_GRAY);
		panel.add(diopterTextField, "flowx,cell 2 1,alignx right");
		diopterTextField.setColumns(3);
		
		JLabel label_6 = new JLabel("דיופטר:");
		label_6.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(label_6, "cell 2 1,alignx center");
		
		typeTextField = new JTextField();
		typeTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		typeTextField.setBackground(Color.LIGHT_GRAY);
		typeTextField.setEditable(false);
		panel.add(typeTextField, "cell 2 1,alignx right,aligny center");
		typeTextField.setColumns(8);
		
		JLabel label_4 = new JLabel("סוג עדשה:");
		label_4.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(label_4, "cell 2 1,alignx center");
		
		quantityTextField = new JTextField();
		quantityTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		quantityTextField.setHorizontalAlignment(SwingConstants.CENTER);
		quantityTextField.setBackground(Color.LIGHT_GRAY);
		quantityTextField.setEditable(false);
		panel.add(quantityTextField, "flowx,cell 3 1,alignx right");
		quantityTextField.setColumns(3);
		
		JLabel label_5 = new JLabel("כמות:");
		label_5.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(label_5, "cell 3 1,alignx center");
		
		orderDateTextField = new JTextField();
		orderDateTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		orderDateTextField.setHorizontalAlignment(SwingConstants.RIGHT);
		orderDateTextField.setEditable(false);
		orderDateTextField.setBackground(Color.LIGHT_GRAY);
		panel.add(orderDateTextField, "cell 3 1,alignx right,aligny center");
		orderDateTextField.setColumns(8);
		
		JLabel label_1 = new JLabel("תאריך:");
		label_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(label_1, "cell 3 1,alignx center");
		
		orderIDTextField = new JTextField();
		orderIDTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		orderIDTextField.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(orderIDTextField, "cell 3 1,alignx right,aligny center");
		orderIDTextField.setColumns(8);
		
		JLabel label = new JLabel("פק''ע:");
		label.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(label, "cell 3 1,alignx right");
		
		masterPanel = new JPanel(){@Override
			public void paintComponent(Graphics g) {
	        Graphics2D g2d = (Graphics2D) g;
	        Color color1 = getBackground();
	        Color color2 = color1.darker();
	        int w = getWidth();
	        int h = getHeight(); 
	        GradientPaint gp = new GradientPaint(
	                0, 0, color1,
	                0, h, color2);

	        g2d.setPaint(gp);
	        g2d.fillRect(0, 0, w, h);
	        }};
		masterPanel.setBackground(new Color(173, 216, 230));
		masterPanel.setBorder(new TitledBorder(null, "\u05E4\u05E1\u05D9\u05DC\u05D5\u05EA\\\u05DC\u05D9\u05D8\u05D5\u05E9 \u05D7\u05D5\u05D6\u05E8", TitledBorder.RIGHT, TitledBorder.TOP, null, null));
		panel.add(masterPanel, "cell 2 3 2 1,growx,aligny center");
		masterPanel.setLayout(new CardLayout(0, 0));
		// TODO Auto-generated constructor stub
		
		
		
		this.hydrophilicReport = new QCHydrophilicReport();				
		this.hydrophobicReport = new QCHydrophobicReport();		
		this.pmmaReport = new QCPMMAReport();		
		this.presbiaReport = new QCPresbiaReport();
		
				
				masterPanel.add(hydrophilicReport, "Hydrophilic");
				masterPanel.add(hydrophobicReport, "Hydrophobic");
				masterPanel.add(pmmaReport, "PMMA");
				masterPanel.add(presbiaReport, "Presbia");
				
				// test visibility
				
				reportLayoutManager = (CardLayout)(masterPanel.getLayout());
				masterPanel.setVisible(false);
		
		steralizationPanel = new JPanel(){@Override
			public void paintComponent(Graphics g) {
	        Graphics2D g2d = (Graphics2D) g;
	        Color color1 = getBackground().darker();
	        Color color2 = color1.darker();
	        int w = getWidth();
	        int h = getHeight(); 
	        GradientPaint gp = new GradientPaint(
	                0, 0, color1,
	                0, h, color2);

	        g2d.setPaint(gp);
	        g2d.fillRect(0, 0, w, h);
	        }};
		steralizationPanel.setBackground(new Color(173, 216, 230));
		steralizationPanel.setBorder(new TitledBorder(null, "\u05E2\u05D9\u05E7\u05D5\u05E8", TitledBorder.RIGHT, TitledBorder.TOP, null, null));
		panel.add(steralizationPanel, "cell 2 4,grow");
		steralizationPanel.setLayout(new MigLayout("", "[][grow][]", "[top][::100px][bottom]"));
		
		steralizationTextField = new JTextField();
		steralizationTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		steralizationPanel.add(steralizationTextField, "flowx,cell 1 1,alignx center,aligny center");
		steralizationTextField.setColumns(10);
		
		JLabel label_11 = new JLabel("אצוות עיקור:");
		label_11.setFont(new Font("Tahoma", Font.PLAIN, 13));
		steralizationPanel.add(label_11, "cell 1 1,alignx center,aligny center");
		steralizationPanel.setVisible(false);
		
		deviationsJPanel = new JPanel(){@Override
			public void paintComponent(Graphics g) {
	        Graphics2D g2d = (Graphics2D) g;
	        Color color1 = getBackground().darker();
	        Color color2 = color1.darker();
	        int w = getWidth();
	        int h = getHeight(); 
	        GradientPaint gp = new GradientPaint(
	                0, 0, color1,
	                0, h, color2);

	        g2d.setPaint(gp);
	        g2d.fillRect(0, 0, w, h);
	        }};
		deviationsJPanel.setBackground(new Color(173, 216, 230));
		deviationsJPanel.setBorder(new TitledBorder(null, "\u05E1\u05D8\u05D9\u05D5\u05EA \u05DE\u05D3\u05D9\u05D5\u05E4\u05D8\u05E8", TitledBorder.RIGHT, TitledBorder.TOP, null, new Color(59, 59, 59)));
		panel.add(deviationsJPanel, "flowx,cell 3 4,growx,aligny center");
		deviationsJPanel.setLayout(new MigLayout("", "[grow,left][][10px:n:20px][][10px:n:20px][][10px:n:20px][][10px:n:20px][][grow,right]", "[][][][]"));
		
		JLabel elseDeviationLabel = new JLabel("אחר");
		elseDeviationLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		deviationsJPanel.add(elseDeviationLabel, "cell 1 1,alignx center");
		
		JLabel label_10 = new JLabel("+1.0");
		label_10.setFont(new Font("Tahoma", Font.PLAIN, 13));
		deviationsJPanel.add(label_10, "cell 3 1,alignx center");
		
		JLabel label_9 = new JLabel("+0.5");
		label_9.setFont(new Font("Tahoma", Font.PLAIN, 13));
		deviationsJPanel.add(label_9, "cell 5 1,alignx center");
		
		JLabel label_8 = new JLabel("-0.5");
		label_8.setFont(new Font("Tahoma", Font.PLAIN, 13));
		deviationsJPanel.add(label_8, "cell 7 1,alignx center");
		
		JLabel label_7 = new JLabel("-1.0");
		label_7.setFont(new Font("Tahoma", Font.PLAIN, 13));
		deviationsJPanel.add(label_7, "cell 9 1,alignx center");
		
		elseDeviationTextField = new JTextField();
		elseDeviationTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		elseDeviationTextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		elseDeviationTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				steralizationTextField.requestFocus();
			}
		});
		elseDeviationTextField.setHorizontalAlignment(SwingConstants.CENTER);
		elseDeviationTextField.setText("0");
		deviationsJPanel.add(elseDeviationTextField, "cell 1 2,alignx center");
		elseDeviationTextField.setColumns(3);
		
		plus10DeviationTextField = new JTextField();
		plus10DeviationTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		plus10DeviationTextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		plus10DeviationTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				elseDeviationTextField.requestFocus();
			}
		});
		plus10DeviationTextField.setText("0");
		plus10DeviationTextField.setHorizontalAlignment(SwingConstants.CENTER);
		deviationsJPanel.add(plus10DeviationTextField, "cell 3 2,alignx center");
		plus10DeviationTextField.setColumns(3);
		
		plus05DeviationTextField = new JTextField();
		plus05DeviationTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		plus05DeviationTextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		plus05DeviationTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				plus10DeviationTextField.requestFocus();
			}
		});
		plus05DeviationTextField.setText("0");
		plus05DeviationTextField.setHorizontalAlignment(SwingConstants.CENTER);
		deviationsJPanel.add(plus05DeviationTextField, "cell 5 2,alignx center");
		plus05DeviationTextField.setColumns(3);
		
		minus05DeviationTextField = new JTextField();
		minus05DeviationTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		minus05DeviationTextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		minus05DeviationTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				plus05DeviationTextField.requestFocus();
			}
		});
		minus05DeviationTextField.setText("0");
		minus05DeviationTextField.setHorizontalAlignment(SwingConstants.CENTER);
		deviationsJPanel.add(minus05DeviationTextField, "cell 7 2,alignx center");
		minus05DeviationTextField.setColumns(3);
		
		minus10DeviationTextField = new JTextField();
		minus10DeviationTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		minus10DeviationTextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		minus10DeviationTextField.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				minus05DeviationTextField.requestFocus();
			}
		});
		minus10DeviationTextField.setText("0");
		minus10DeviationTextField.setHorizontalAlignment(SwingConstants.CENTER);
		deviationsJPanel.add(minus10DeviationTextField, "cell 9 2,alignx center");
		minus10DeviationTextField.setColumns(3);
		deviationsJPanel.setVisible(false);
		
		JButton exitButton = new JButton("יציאה מהמערכת");
		exitButton.setFont(new Font("Tahoma", Font.PLAIN, 13));
		add(exitButton, "flowx,cell 1 1,alignx center");
		exitButton.setIcon(new ImageIcon(QCReportPanel.class.getResource("/graphics/exit.png")));
		
		createReportButton = new JButton("דווח על בקרה");
		createReportButton.setFont(new Font("Tahoma", Font.PLAIN, 13));
		add(createReportButton, "cell 1 1");
		createReportButton.setIcon(new ImageIcon(QCReportPanel.class.getResource("/graphics/apply.png")));
		createReportButton.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				// Confirm validity of the report
				if (diopterTextField.getText().isEmpty() || steralizationTextField.getText().isEmpty())
				{
					JOptionPane.showMessageDialog(myFrame,
						    "חסרים פרטים עבור פק''ע בדוח זה.","שגיאה",JOptionPane.ERROR_MESSAGE);
				}
				else
				if (!everythingIsValid())
				{
					JOptionPane.showMessageDialog(myFrame,
						    "כמויות מדווחות עבור דוח זה אינן תקינות.","שגיאה",JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					// Assemble report from all the report panels
					QCReport report = createReport();
					
					if (  (report.getOrderQuantity() < (report.getRepolishSetFlag() + report.getFailSetFlag())) || (report.getOrderQuantity() < (report.getDeviationCountElse()+report.getDeviationCountMinus05()+report.getDeviationCountMinus10()+report.getDeviationCountPlus05()+report.getDeviationCountPlus10())))
					{
						// quantities dont match
						JOptionPane.showMessageDialog(myFrame,
							    "כמויות מדווחות עבור דוח זה אינן תקינות.","שגיאה",JOptionPane.ERROR_MESSAGE);
						
					}
					else
					{
						// send to server
						ArrayList<Object> message = new ArrayList<Object>();
						message.add("addNewQCReport");
						message.add(report);
						try 
						{
							ArrayList<Object> response = myFrame.qcClient.call(message);
							
							if (response.get(0).toString().equalsIgnoreCase("Valid") )
							{
								if (response.get(2).toString().equalsIgnoreCase("No RepolishSet"))
								{
									JOptionPane.showMessageDialog(myFrame,
										    "דיווח בוצע.","אישור",JOptionPane.INFORMATION_MESSAGE);
									myFrame.myTabbedPane.setSelectedIndex(0);
								}
								else
								{
									JOptionPane.showMessageDialog(myFrame,
										    "דיווח בוצע, נוצרה פק''ע חדשה במערכת לליטוש חוזר.","אישור",JOptionPane.INFORMATION_MESSAGE);
									myFrame.myTabbedPane.setSelectedIndex(0);
								}
								
								
							}
							else
							{
								// SQL Error
								JOptionPane.showMessageDialog(myFrame,
									    "ארעה שגיאה בבסיס הנתונים.","שגיאה",JOptionPane.ERROR_MESSAGE);
							}
							
						}
						catch (Exception e)
						{
							
							JOptionPane.showMessageDialog(myFrame,
								    "ארעה שגיאה בחיבור לשרת.","שגיאה",JOptionPane.ERROR_MESSAGE);
							//e.printStackTrace();
						}
						
						
						
						// remove orderID from the table
						
						myFrame.myListOfOrders.removeFromTable(orderIDTextField.getText());
						// 	clear all fields
						
						clearFields();
					}

				}
					
			}
		});
		createReportButton.setToolTipText("");
		exitButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e)
			{
				Object[] options = {"כן","לא"};
        		int n = JOptionPane.showOptionDialog(myFrame, "האם ברצונך לצאת מהמערכת?","אישור יציאה", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,"");
        		
        		if (n==0)
        		{
        			if (myFrame.qcClient != null)
                    {
                      try
                      {
                    	  myFrame.qcClient.close();
                      }
                      catch (Exception ignore)
                      {}
                     }
            		
            		System.exit(0);
        		}
			}
		});
		

		
	}
/**
 * Clears all GUI Fields
 */
	protected void clearFields()
	{
		hydrophilicReport.clearFields();
		hydrophobicReport.clearFields();
		pmmaReport.clearFields();
		presbiaReport.clearFields();
		
		orderIDTextField.setText("");
		orderDateTextField.setText("");
		typeTextField.setText("");
		diopterTextField.setText("");
		quantityTextField.setText("");
		
		// steralization
		steralizationTextField.setText("");
		
		//deviation
		minus05DeviationTextField.setText("0");
		minus10DeviationTextField.setText("0");
		plus05DeviationTextField.setText("0");
		plus10DeviationTextField.setText("0");
		elseDeviationTextField.setText("0");
		
		
		setReportVisible(false);

		
	}
/**
 * Sets the GUI elements visible.
 */
	private void setReportVisible(boolean b)
	{
		masterPanel.setVisible(b);
		deviationsJPanel.setVisible(b);
		steralizationPanel.setVisible(b);
		
	}

	/**
	 * Test for input validity
	 */
	protected boolean everythingIsValid() 
	{
		// test report panel variables
		boolean reportResult = true;
		
		switch (currentReport)
		{
			case "Hydrophilic":
			{
				reportResult = hydrophilicReport.everythingIsValid();		
				break;
			}
			
			case "Hydrophobic":
			{
				reportResult = hydrophobicReport.everythingIsValid();		
				break;
			}
			
			case "PMMA":
			{
				reportResult = pmmaReport.everythingIsValid();		
				break;
			}

			case "Presbia":
			{
				reportResult = presbiaReport.everythingIsValid();		
				break;
			}
			
		}
		
		if (!reportResult)
			return false;
		
			// test deviations
			boolean deviationsResult;
			deviationsResult = (utilities.Services.isNumeric(elseDeviationTextField.getText()) &&
					utilities.Services.isNumeric(minus05DeviationTextField.getText()) &&
					utilities.Services.isNumeric(minus10DeviationTextField.getText()) &&
					utilities.Services.isNumeric(plus05DeviationTextField.getText()) &&
					utilities.Services.isNumeric(plus10DeviationTextField.getText()) );
			if (!deviationsResult)
				return false;
			else
			return true;
		
	}
	
	/**
	 * Create a quality control report
	 */

	protected QCReport createReport()
	{
		QCReport report = new QCReport();
		report.setOrderID(orderIDTextField.getText());
		report.setQCDateAsString(utilities.Services.getToday());			
		report.setSterilizationID(steralizationTextField.getText());	
		report.setDeviationCountElse(Integer.parseInt(elseDeviationTextField.getText()));
		report.setDeviationCountMinus05(Integer.parseInt(minus05DeviationTextField.getText()));
		report.setDeviationCountMinus10(Integer.parseInt(minus10DeviationTextField.getText()));
		report.setDeviationCountPlus05(Integer.parseInt(plus05DeviationTextField.getText()));
		report.setDeviationCountPlus10(Integer.parseInt(plus10DeviationTextField.getText()));
		
		FailSet failset;
		int deviationFails= sumOfDeviations();
		
		switch (currentReport)
		{
		
		
			case "Hydrophilic":
			{
				failset = hydrophilicReport.getFailSet();
				failset.setDiopterDeviation(String.valueOf(deviationFails));
				
				failset.setFailsetFlag(failset.getFailsetFlag() + deviationFails);
				
				report.setFailset(failset);
				//report.setFailset(hydrophilicReport.getFailSet());
				report.getFailset().setOrderID(orderIDTextField.getText());
					
				
				report.setRepolishset(hydrophilicReport.getRepolishSet());
				report.getRepolishset().setOriginalOrderID(orderIDTextField.getText());
				
				report.setFailSetFlag(report.getFailset().getFailsetFlag());
				report.setRepolishSetFlag(report.getRepolishset().getRepolishFlag());
				
				break;
			}
			
			case "Hydrophobic":
			{
				
				failset = hydrophobicReport.getFailSet();
				failset.setDiopterDeviation(String.valueOf(deviationFails));
				
				failset.setFailsetFlag(failset.getFailsetFlag() + deviationFails);
				
				report.setFailset(failset);				
			
				report.getFailset().setOrderID(orderIDTextField.getText());
				
				report.setRepolishset(hydrophobicReport.getRepolishSet());
				report.getRepolishset().setOriginalOrderID(orderIDTextField.getText());
				
				report.setFailSetFlag(report.getFailset().getFailsetFlag());
				report.setRepolishSetFlag(report.getRepolishset().getRepolishFlag());
				
				break;
				
			}
			
			case "PMMA":
			{
				
				failset = pmmaReport.getFailSet();
				failset.setDiopterDeviation(String.valueOf(deviationFails));
				
				failset.setFailsetFlag(failset.getFailsetFlag() + deviationFails);
				
				report.setFailset(failset);
				
				report.setRepolishset(pmmaReport.getRepolishSet());
				
				report.getFailset().setOrderID(orderIDTextField.getText());
				report.getRepolishset().setOriginalOrderID(orderIDTextField.getText());
				
				report.setFailSetFlag(report.getFailset().getFailsetFlag());
				report.setRepolishSetFlag(report.getRepolishset().getRepolishFlag());
				
				break;
			}

			case "Presbia":
			{
				failset = presbiaReport.getFailSet();
				failset.setDiopterDeviation(String.valueOf(deviationFails));
				
				failset.setFailsetFlag(failset.getFailsetFlag() + deviationFails);
				
				report.setFailset(failset);
				
				report.getFailset().setOrderID(orderIDTextField.getText());
				
				report.setRepolishset(presbiaReport.getRepolishSet());
				report.getRepolishset().setOriginalOrderID(orderIDTextField.getText());
				
				report.setFailSetFlag(report.getFailset().getFailsetFlag());
				report.setRepolishSetFlag(report.getRepolishset().getRepolishFlag());
				
				break;
			}
		
		
			

		}	
		
		ProductionOrder repolishOrder = new ProductionOrder();
		if (report.getRepolishset().getRepolishOrderID().equals("") || report.getRepolishset().getRepolishOrderID().equals("0") )
		{
			// do nothing
		}
		else
		{
			repolishOrder.setOrderID(Integer.parseInt( report.getRepolishset().getRepolishOrderID() ));
		}
		
		repolishOrder.setType(typeTextField.getText());
		repolishOrder.setDiopter(Float.parseFloat(diopterTextField.getText()));
		repolishOrder.setDateAsString(utilities.Services.getToday());
		repolishOrder.setQuantity(report.getRepolishSetFlag());
		repolishOrder.setStatusID(5);
		
		report.getRepolishset().setRepolishOrder(repolishOrder);
		
		report.setOrderQuantity(Integer.parseInt(quantityTextField.getText()));
		return report;
	}

	/**
	 * Initializes the report GUI according to the order it receives as a parameter.
	 *  
	 */
	public void initializeByListOfOrdersTab(ProductionOrder productionOrder)
	{
		orderIDTextField.setText(String.valueOf(productionOrder.getOrderID()));
		orderDateTextField.setText(productionOrder.getDateAsString());
		typeTextField.setText(productionOrder.getType());
		diopterTextField.setText(String.valueOf(productionOrder.getDiopter()));
		quantityTextField.setText(String.valueOf(productionOrder.getQuantity()));
		
		
		myFrame.myTabbedPane.setSelectedIndex(1); // Switch to createReportPanel
		quantityTextField.requestFocus();
		initializeReportPanels(productionOrder.getType());
		
	
		
	}

	/**
	 * Sets the appropriate Quality report panel by the order type to be visible
	 */
	private void initializeReportPanels(String type) 
	{
		reportLayoutManager.show(masterPanel, currentReport=utilities.Services.convertTypeToFamily(type));
		setReportVisible(true);
		
	}
	
	/**
	 * Sum the deviations in the report
	 */
	private int sumOfDeviations()
	{
		return (Integer.parseInt(elseDeviationTextField.getText()) + Integer.parseInt(minus05DeviationTextField.getText()) + Integer.parseInt(minus10DeviationTextField.getText()) + Integer.parseInt(plus05DeviationTextField.getText()) + Integer.parseInt(plus10DeviationTextField.getText()) );
	}

	@Override
	public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Color color1 = getBackground();
    Color color2 = color1.darker();
    int w = getWidth();
    int h = getHeight(); 
    GradientPaint gp = new GradientPaint(
            0, 0, color1,
            0, h, color2);

    g2d.setPaint(gp);
    g2d.fillRect(0, 0, w, h);
    }
}
